<?php
/**
 * Description of menu
 *
 * @author Dmitry
 */
class Models_Menu extends Access_Db {
    function getMenu($visible = "visible='1' and ")
    {
        $select = array(
            'id',
            'name',
            $this->lang . '_menu_id',
            $this->lang . '_article_id'
        );
        $table = $this->lang . '_menu';
        $where = $visible . $this->lang . "_menu_id='0' and $this->lang" . "_article_id='0' ORDER BY position ASC";
        $this->select($select, $table, $where);
        $result = array();
        while($row = $this->getSelected())
        {
            $result[] = $row;
        }
        return $result;
    }
    
    function getMenuId($id, $visible = "visible='1' and ")
    {
        $select = array(
            'id',
            'name',
            $this->lang . '_menu_id',
            $this->lang . '_article_id'
        );
        $table = $this->lang . '_menu';
        $where = $visible . $this->lang . "_article_id='$id'";
        $this->select($select, $table, $where);
        return $this->getSelected();
    }

    function getMenuHeader($id)
    {
        $select = array(
            'id',
            'name',
            $this->lang . '_menu_id'
        );
        $table = $this->lang . '_menu';
        $where = "id='$id' and " . $this->lang . "_menu_id='0' and $this->lang" . "_article_id='0'";
        $this->select($select, $table, $where);
        $row = $this->getSelected();
        return $row;
    }
    
    function getSubMenu($id, $visible = "visible='1' and ")
    {
        $select = array(
            'id',
            $this->lang . '_article_id',
            'name',
            $this->lang . '_menu_id'
        );
        $table = $this->lang . '_menu';
        $where = $visible . $this->lang . "_menu_id='$id' ORDER BY position ASC";
        $this->select($select, $table, $where);
        $result = array();
        while($row = $this->getSelected())
        {
            $result[] = $row;
        }
        return $result;
    }
    
    function validateMenuHeader($arr)
    {
        $errors = array();
        if((strlen($arr['0']) > 32) or (strlen($arr['0']) < 1))
        {
            $errors[] = 'Name must be at least 1 and up to 32 characters.';
            return $errors;
        }
        return FALSE;
    }

    function setMenuHeader($arr) //set title of menu block
    {
        $query = "INSERT INTO $this->lang" . "_menu (name, visible)
                    VALUES (?,?)";
        $this->prepare($query);
        $this->insert($arr);
    }
    
    function editMenuHeader($arr, $id)
    {
        $query = "UPDATE $this->lang" . "_menu SET name='" 
                . $arr['0']. "', visible='" 
                . $arr['1'] . "' WHERE id='$id'";
        $this->execQuery($query);
    }

    function showSiteMap()
    {
        //TODO
    }
}